const { Router } = require('express')
const express = require('express')
// const qs = require('querystring')
//路由容器
const productDetail = express.Router()
const axios = require('axios')
const { RSA_NO_PADDING } = require('constants')
// 加入购物车
productDetail.get('/addCart', (req, res)=>{
    const {count, goodsId, shopId, userId} = req.query
    axios({
        url:'http://www.mianded.top:8081/cart/addCart',
        method:'POST',
        params:{
            count: count,
            goodsId: goodsId,
            shopId: shopId,
            userId: userId
        },
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data => {
        // console.log(data);
        res.status(200).json({
            data:data.data
        })
    })
    .catch(err => {
        console.log(err);
    })
})
//立即购买
// productDetail.get('')


//遍历评论的接口
productDetail.get('/getAllCommentsByShopId', (req, res) => {
    const {shopId} = req.query
    axios({
        url:'http://www.mianded.top:8081/comment/getAllCommentsByShopId',
        method:'POST',
        params:{
            shopId:shopId
        },
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data => {
        // console.log(51);
        // console.log(data.data.data);
        res.status(200).json({
            data:data.data.data
        })
    })
    .catch(err => {
        // console.log(err);
        res.status(200).json({
            err:err
        })
    })
})

//查看商品详情
productDetail.get('/inGoodsPage', (req, res) => {
    const goodsId = req.query.goodsId
    axios({
        url:'http://www.mianded.top:8081/goods/inGoodsPage',
        method:'GET',
        params:{
            goodsId: goodsId
        },
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data => {
        console.log(data);
        res.status(200).json({
            data:data.data
        })
        // res.send(data.data)
    })
    .catch(err => {
        console.log(err);
        res.status(200).json({
            err:err
        })
        // res.send(err)

    })
})

//收藏宝贝
productDetail.get('/collection/addCollection', (req, res) => {
    const {userId, shopId, goodsId} = req.query
    axios({
        url:'http://www.mianded.top:8081/collection/addCollection',
        method:'POST',
        params:{
            userId:userId,
            shopId:shopId,
            goodsId:goodsId
        },
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data=>{
        console.log(data);
        console.log(data.data);
        res.status(200).json({
            data:data.data
        })
    })
    .catch(err => {
        console.log(err);
        res.status(200).json({
            err:err
        })
    })
})

//取消收藏商品
productDetail.get('/collection/deleteCollection', (req, res) => {
    const collectionId = req.query
    axios({
        url:'http://www.mianded.top:8081/collection/deleteCollection',
        method:'DELETE',
        params:{
            collectionId:collectionId
        },
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data=>{
        console.log(data);
        res.status(200).json({
            data:data
        })
    })
    .catch(err => {
        console.log(err);
        res.status(200).json({
            err:err
        })
    })
})


module.exports=productDetail;